package com.zhonghui.modlules.user.eshopService.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eshop.service.Impl.BaseServiceImpl;
import com.zhonghui.common.eshopDozer.config.IGenerator;
import com.zhonghui.common.eshopEnum.ShopCommonEnum;
import com.zhonghui.modlules.user.eshopDomain.UserAddress;
import com.zhonghui.modlules.user.eshopMapper.UserAddressMapper;
import com.zhonghui.modlules.user.eshopParam.AddressParam;
import com.zhonghui.modlules.user.eshopService.UserAddressService;
import com.zhonghui.modlules.user.eshopVo.UserAddressQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(
        rollbackFor = {Exception.class}
)
public class UserAddressServiceImpl extends BaseServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {
    private final UserAddressMapper userAddressMapper;
    private final IGenerator generator;

    public void setDefault(Long uid, Long addressId) {
        UserAddress address = new UserAddress();
        address.setIsDefault(ShopCommonEnum.DEFAULT_0.getValue());
        LambdaQueryWrapper<UserAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserAddress::getUid,uid);
        userAddressMapper.update(address,wrapper);
        UserAddress userAddress = new UserAddress();
        userAddress.setIsDefault(ShopCommonEnum.DEFAULT_1.getValue());
        userAddress.setId(addressId);
        this.userAddressMapper.updateById(userAddress);
    }

    public UserAddressQueryVo getDetail(Long id) {
        return (UserAddressQueryVo)this.generator.convert(this.getById(id), UserAddressQueryVo.class);
    }

    public List<UserAddressQueryVo> getList(Long uid, int page, int limit) {
        Page<UserAddress> pageModel = new Page((long)page, (long)limit);
        IPage<UserAddress> pageList = ((LambdaQueryChainWrapper)this.lambdaQuery().eq(UserAddress::getUid, uid)).page(pageModel);
        return this.generator.convert(pageList.getRecords(), UserAddressQueryVo.class);
    }


    public Long addAndEdit(Long uid, AddressParam param){
        UserAddress userAddress = UserAddress.builder().city( param.getAddress().getCity() ).
                cityId( param.getAddress().getCityId() ).
                district( param.getAddress().getDistrict() ).
                province( param.getAddress().getProvince() ).
                detail( param.getDetail() ).
                uid( uid ).phone( param.getPhone() ).
                postCode( param.getPost_code() ).realName( param.getReal_name() )
                .build();
        if ("true".equals( param.getIs_default() )){
            userAddress.setIsDefault( ShopCommonEnum.DEFAULT_1.getValue() );
            UserAddress address = new UserAddress();
            address.setIsDefault( ShopCommonEnum.DEFAULT_0.getValue() );
            baseMapper.update( address,(Wrapper)
                    (new LambdaQueryWrapper<UserAddress>()).eq( UserAddress::getUid,uid ) );
        }else {
            userAddress.setIsDefault( ShopCommonEnum.DEFAULT_0.getValue() );
        }
        if (StrUtil.isBlank( param.getId() )){
            this.save( userAddress );
        }else{
            userAddress.setId( Long.valueOf( param.getId() ) );
            this.updateById( userAddress );
        }
        return userAddress.getId();

    }


    public UserAddressServiceImpl(final UserAddressMapper userAddressMapper, final IGenerator generator) {
        this.userAddressMapper = userAddressMapper;
        this.generator = generator;
    }

}
